//
//  RWBChouseTimeView.m
//  Renwubang
//
//  Created by 张露杰 on 17/12/12.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "RWBChouseTimeView.h"
typedef void(^RWBChouseTimeViewBlock)(NSDate *);
@interface RWBChouseTimeView()
@property (nonatomic, strong) UIView *pickerView;
@property (nonatomic, strong) UIDatePicker *picker;
@end
@implementation RWBChouseTimeView
{
    RWBChouseTimeViewBlock _complete;
    UIDatePickerMode _datePickerMode;
    NSDate *_maxDate;
    NSDate *_minDate;
}
- (instancetype)initWithComplete:(void(^)(NSDate *date))complete
{
    self = [super init];
    if (self) {
        if (complete) {
            _complete = complete;
        }
        [self creatView];
    }
    return self;
}

- (instancetype)initWithDatePickerMode:(UIDatePickerMode )datePickerMode maxDate:(NSDate*)maxDate minDate:(NSDate*)minDate Complete:(void(^)(NSDate *date))complete
{
    self = [super init];
    if (self) {
        if (complete) {
            _complete = complete;
        }
        _datePickerMode = datePickerMode;
        _maxDate = maxDate;
        _minDate = minDate;
        [self creatView];
    }
    return self;
}

-(void)creatView
{
    self.frame = CGRectMake(0, 0, SCREENW, SCREENH);
    
    /**
     底部遮罩
     */
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.5;
    [self addSubview:backGroundView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [backGroundView addGestureRecognizer:tap];
    
    [self addSubview:self.pickerView];
}

-(UIView *)pickerView
{
    if (!_pickerView) {
        _pickerView =  [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 300*screenW_6)];
        _pickerView.backgroundColor = [UIColor whiteColor];
        
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        shareBtn.frame = CGRectMake(self.frame.size.width - 50*screenW_6, 0, 50*screenW_6, 50*screenW_6);
        [shareBtn setTitle:@"确认" forState:UIControlStateNormal];
        [shareBtn setTitleColor:mainColor forState:UIControlStateNormal];
        shareBtn.titleLabel.font = [UIFont systemFontOfSize:14*screenW_6];
        [_pickerView addSubview:shareBtn];
        [shareBtn addTarget:self action:@selector(chouseTitle) forControlEvents:UIControlEventTouchUpInside];
        
        [_pickerView addSubview:self.picker];
    }
    return _pickerView;
}

-(UIDatePicker *)picker
{
    if (!_picker) {
        _picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,50*screenW_6,self.frame.size.width,self.pickerView.frame.size.height - 50*screenW_6)];
        _picker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_ch"];
        // 设置时区，中国在东八区
        _picker.timeZone = [NSTimeZone timeZoneWithName:@"GTM+8"];
        //        _picker.datePickerMode = UIDatePickerModeDate;
        NSDate *minDate = [NSDate date];
        _picker.minimumDate = minDate;
        _picker.datePickerMode = UIDatePickerModeDateAndTime;
        if (_datePickerMode) {
            _picker.datePickerMode = _datePickerMode;
        }
        _picker.maximumDate = _maxDate;
        _picker.minimumDate = _minDate;
//        _picker = [[AADatePicker alloc] initWithFrame:CGRectMake(0,50*screenW_6,self.frame.size.width,self.pickerView.frame.size.height - 50*screenW_6) maxDate:nil minDate:[NSDate dateWithTimeIntervalSinceNow:30*60] showValidDatesOnly:YES];
//        _picker.backgroundColor = [UIColor whiteColor];
//        _picker.delegate = self;
    }
    return _picker;
}

-(void)dismiss
{
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.alpha = 0.0;
                         _pickerView.frame = CGRectMake(0, self.frame.size.height, _pickerView.frame.size.width, _pickerView.frame.size.height);
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                     }];
}

-(void)showOnView:(UIView *)view
{
    if (!view) {
        return;
    }
    [view addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        _pickerView.frame = CGRectMake(0, self.frame.size.height - _pickerView.frame.size.height, _pickerView.frame.size.width, _pickerView.frame.size.height);
    } completion:nil];
}

///**
// *  AADatePickerDelegate
// *
// *  @param sender AADatePicker
// */
//
//-(void)dateChanged:(AADatePicker *)sender
//{
//    NSString *dateString = [NSDateFormatter localizedStringFromDate:sender.date
//                                                          dateStyle:NSDateFormatterShortStyle
//                                                          timeStyle:NSDateFormatterMediumStyle];
//    NSLog(@"%@",dateString);
//    
//    [self.dateLabel setText:dateString];
//}

-(void)chouseTitle
{
    _complete(_picker.date);
    [self dismiss];
}

@end
